package cn.mesmile.workflow.process.controller;

import cn.mesmile.workflow.commom.Query;
import cn.mesmile.workflow.commom.R;
import cn.mesmile.workflow.process.bo.OperationTaskBO;
import cn.mesmile.workflow.process.bo.StartProcessBO;
import cn.mesmile.workflow.process.qo.WorkFlowQO;
import cn.mesmile.workflow.process.service.WorkFlowService;
import cn.mesmile.workflow.process.vo.*;
import cn.mesmile.workflow.process.wrapper.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zb
 * @Description
 */
@Api(value = "工作流相关接口", tags = "工作流相关接口")
@AllArgsConstructor
@RequestMapping("/api/v1/workflow")
@RestController
public class WorkFlowController {

    private final WorkFlowService workFlowService;

    @ApiOperation("查询流程定义列表")
    @GetMapping("/listProcessDefinition")
    public R listProcessDefinition(Query query, WorkFlowQO workFlowQO) {
        IPage<ProcessDefinition> processDefinitionPage = workFlowService.listProcessDefinition(query, workFlowQO);
        IPage<ProcessDefinitionVO> processDefinitionVO = ProcessDefinitionWrapper.build().pageVO(processDefinitionPage);
        return R.data(processDefinitionVO);
    }

    @ApiOperation("发起流程")
    @PostMapping("/startProcessById")
    public R startProcessById(@RequestBody StartProcessBO startProcessBO){
        String processDefId = startProcessBO.getProcessDefId();
        Map<String, Object> variables = startProcessBO.getVariables();
        if (StringUtils.isNotBlank(processDefId)){
            R.fail("缺少必要参数流程定义id(processDefId)");
        }
        if (variables == null){
            variables = new HashMap<>(16);
        }
        ProcessInstance processInstance = workFlowService.startProcessInstanceByDefId(processDefId, variables);
        return R.data(processInstance.getId());
    }

    @ApiOperation("查询 我的待办任务 列表")
    @GetMapping("/listTodoTask")
    public R listTodoTask(Query query, WorkFlowQO workFlowQO){
        IPage<Task> taskPage = workFlowService.listTodoTask(query, workFlowQO);
        IPage<TodoTaskVO> todoTaskVOPage = TodoTaskWrapper.build().pageVO(taskPage);
        return R.data(todoTaskVOPage);
    }

    @ApiOperation("查询 我的待签任务 列表")
    @GetMapping("/listClaimTask")
    public R listClaimTask(Query query, WorkFlowQO workFlowQO){
        IPage<Task> taskIPage = workFlowService.listClaimTask(query, workFlowQO);
        IPage<ClaimTaskVO> claimTaskVOIPage = ClaimTaskWrapper.build().pageVO(taskIPage);
        return R.data(claimTaskVOIPage);
    }

    @ApiOperation("查询 我的已办流程实例 列表")
    @GetMapping("/listDoneProcess")
    public R listDoneProcess (Query query, WorkFlowQO workFlowQO){
        IPage<HistoricProcessInstance> historicProcessInstancePage = workFlowService.listDoneProcess(query, workFlowQO);
        IPage<DoneProcessVO> doneProcessVOPage = DoneProcessWrapper.build().pageVO(historicProcessInstancePage);
        return R.data(doneProcessVOPage);
    }

    @ApiOperation("查询 我发起的流程实例 列表")
    @GetMapping("/listSendProcess")
    public R listSendProcess(Query query, WorkFlowQO workFlowQO){
        IPage<HistoricProcessInstance> historicProcessInstancePage = workFlowService.listSendProcess(query, workFlowQO);
        IPage<SendProcessVO> sendProcessVOPage = SendProcessWrapper.build().pageVO(historicProcessInstancePage);
        return R.data(sendProcessVOPage);
    }

    @ApiOperation("完成任务 通过/驳回 ")
    @PostMapping("/completeTask")
    public R completeTask(@RequestBody OperationTaskBO operationTaskBO) {
        return workFlowService.completeTask(operationTaskBO);
    }

    @ApiOperation("催办任务处理人，处理任务,这里是发送通知消息")
    @PostMapping("/urgeTask")
    public R urgeTask(@RequestBody OperationTaskBO operationTaskBO){
        workFlowService.urgeTask(operationTaskBO);
        return R.status(true);
    }

    @ApiOperation("转办任务")
    @PostMapping("/transferTask")
    public R transferTask(@RequestBody OperationTaskBO operationTaskBO){
        workFlowService.transferTask(operationTaskBO);
        return R.success("转办任务成功");
    }

    @ApiOperation("委托任务")
    @PostMapping("/delegateTask")
    public R delegateTask(@RequestBody OperationTaskBO operationTaskBO){
        workFlowService.delegateTask(operationTaskBO);
        return R.success("委托任务成功");
    }

    @ApiOperation("终止任务,跳转流程到结束节点")
    @PostMapping("/terminationTask")
    public R  terminationTask (@RequestBody OperationTaskBO operationTaskBO){
        workFlowService.terminationTask(operationTaskBO);
        return R.success("终止任务成功");
    }

    @ApiOperation("加签任务")
    @PostMapping("/addMultiInstance")
    public R addMultiInstance(@RequestBody OperationTaskBO operationTaskBO){
        workFlowService.addMultiInstance(operationTaskBO);
        return R.success("加签任务成功");
    }

    @ApiOperation("减签任务")
    @PostMapping("/deleteMultiInstance")
    public R deleteMultiInstance(@RequestBody OperationTaskBO operationTaskBO){
        workFlowService.deleteMultiInstance(operationTaskBO);
        return R.success("减签任务成功");
    }

    @ApiOperation("回退任务到指定节点")
    @PostMapping("/rollbackTask")
    public R rollbackTask(@RequestBody OperationTaskBO operationTaskBO){
        workFlowService.rollbackTask(operationTaskBO);
        return R.success("回退任务到指定节点成功");
    }

    @ApiOperation("获取可回退节点的列表")
    @PostMapping("/getBackNodeList")
    public R getBackNodeList(OperationTaskBO operationTaskBO){
        workFlowService.getBackNodeList(operationTaskBO);
        return R.success("获取列表成功");
    }




}
